package com.whlilong.jxc.editor.support;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;

import com.whlilong.jxc.listener.IEditorListener;
import com.whlilong.jxc.listener.ITextKeyChangeListener;
import com.whlilong.jxc.model.Goodsbrand;
import com.whlilong.jxc.model.Goodsinfo;
import com.whlilong.jxc.model.Hsinfo;
import com.whlilong.jxc.model.Keshang;
import com.whlilong.jxc.model.Khinfo;
import com.whlilong.jxc.model.Kstype;
import com.whlilong.jxc.sys.ExtentionInfo;

public class GoodsInfoEditorSupport extends EditingSupport implements
		ITextKeyChangeListener {

	/**
	 * 当前第几列
	 */
	private int currentColumn;

	/**
	 * 是否第一次修改该单元格
	 */
	private boolean isFirst = true;

	/**
	 * 为单元格添加的editor
	 */
	private CellEditor editor;

	/**
	 * 客户类别list
	 */
	private List<Kstype> merchantTypelist = new ArrayList<Kstype>();

	/**
	 * 客户区域list
	 */
	private List<Keshang> districtList = new ArrayList<Keshang>();

	/**
	 * 货商信息list
	 */
	private List<Hsinfo> businessInfoList = new ArrayList<Hsinfo>();

	/**
	 * 商品品牌list
	 */
	private List<Goodsbrand> brandInfoList = new ArrayList<Goodsbrand>();

	/**
	 * Combo初始化
	 */
	private String[] merchantItems, districtItems, businessInfoItems,
			brandInfoItems;

	public GoodsInfoEditorSupport(TableViewer viewer, int column,
			IEditorListener listener) {
		super(viewer);
		comboBoxInitial();
		this.currentColumn = column;
		switch (column) {
		case 2:
		case 3:
		case 9:
		case 10:
		case 11:
		case 12:
		case 13:
		case 14:
		case 15:
			editor = new GoodsInfoTextKeyCellEditor(viewer.getTable(),
					listener, column, this);
			break;
		case 5:
			editor = new GoodsInfoComboBoxCellEditor(viewer.getTable(),
					listener, column, this, districtItems);
			break;
		case 6:
			editor = new GoodsInfoComboBoxCellEditor(viewer.getTable(),
					listener, column, this, merchantItems);
			break;
		case 7:
			editor = new GoodsInfoComboBoxCellEditor(viewer.getTable(),
					listener, column, this, businessInfoItems);
			break;
		case 8:
			editor = new GoodsInfoComboBoxCellEditor(viewer.getTable(),
					listener, column, this, brandInfoItems);
			break;
		default:
			editor = null;
		}
	}

	private void comboBoxInitial() {
		merchantTypelist = ExtentionInfo.getInstance().getKstypeinfo();
		merchantItems = new String[merchantTypelist.size() + 1];
		merchantItems[0] = "不定";
		for (int i = 0; i < merchantTypelist.size(); i++) {
			merchantItems[i + 1] = merchantTypelist.get(i).getKstype();
		}

		districtList = ExtentionInfo.getInstance().getKeshanginfo();
		districtItems = new String[districtList.size() + 1];
		districtItems[0] = "不定";
		for (int i = 0; i < districtList.size(); i++) {
			districtItems[i + 1] = districtList.get(i).getKeshangarea();
		}

		businessInfoList = ExtentionInfo.getInstance().getBusinessInfoList();
		businessInfoItems = new String[businessInfoList.size() + 1];
		businessInfoItems[0] = "不定";
		for (int i = 0; i < businessInfoList.size(); i++) {
			businessInfoItems[i + 1] = businessInfoList.get(i).getHsname();
		}

		brandInfoList = ExtentionInfo.getInstance().getBrandInfo();
		brandInfoItems = new String[brandInfoList.size() + 1];
		brandInfoItems[0] = "不定";
		for (int i = 0; i < brandInfoList.size(); i++) {
			brandInfoItems[i + 1] = brandInfoList.get(i).getGoodsbrandname();
		}

	}

	@Override
	protected boolean canEdit(Object element) {
		return (currentColumn != 1 && currentColumn != 4);
	}

	@Override
	protected CellEditor getCellEditor(Object element) {
		return editor;
	}

	@Override
	protected Object getValue(Object element) {
		Goodsinfo obj = (Goodsinfo) element;
		switch (currentColumn) {
		case 2:
			String id = obj.getGoodsid();
			if (isFirst) {
				((GoodsInfoTextKeyCellEditor) editor).setOldString(id);
				isFirst = false;
			}
			return id;
		case 3:
			return obj.getGoodsname();
		case 4:
			return obj.getPym();
		case 5:
			for (int i = 0; i < districtList.size(); i++)
				if (obj.getAddress().equals(
						districtList.get(i).getKeshangarea()))
					return i + 1;
			return 0;
		case 6:
			for (int i = 0; i < merchantTypelist.size(); i++)
				if (obj.getGoodstpname().equals(
						merchantTypelist.get(i).getKstype()))
					return i + 1;
			return 0;
		case 7:
			for (int i = 0; i < businessInfoList.size(); i++)
				if (obj.getHsname().equals(businessInfoList.get(i).getHsname()))
					return i + 1;
			return 0;
		case 8:
			for (int i = 0; i < brandInfoList.size(); i++)
				if (obj.getGoodsbrandname().equals(
						brandInfoList.get(i).getGoodsbrandname()))
					return i + 1;
			return 0;
		case 9:
			return obj.getGoodshh();
		case 10:
			return obj.getCkjj().toString();
		case 11:
			return obj.getPfj().toString();
		case 12:
			return obj.getHyj().toString();
		case 13:
			return obj.getLsj().toString();
		case 14:
			return obj.getUnit();
		case 15:
			return obj.getGg();
		}

		return null;
	}

	@Override
	protected void setValue(Object element, Object value) {
		Goodsinfo obj = (Goodsinfo) element;
		switch (currentColumn) {
		case 2:
			obj.setGoodsid(String.valueOf(value));
			break;
		case 3:
			obj.setGoodsname(String.valueOf(value));
			break;
		case 4:
			obj.setPym(String.valueOf(value));
			break;
		case 5:
			if (Integer.valueOf(String.valueOf(value)) == 0)
				obj.setAddress("不定");
			for (int i = 0; i < districtList.size(); i++)
				if (Integer.valueOf(String.valueOf(value)) == i + 1)
					obj.setAddress(districtList.get(i).getKeshangarea());
			break;
		case 6:
			if (Integer.valueOf(String.valueOf(value)) == 0)
				obj.setGoodstpname("不定");
			for (int i = 0; i < merchantTypelist.size(); i++)
				if (Integer.valueOf(String.valueOf(value)) == i + 1)
					obj.setGoodstpname(merchantTypelist.get(i).getKstype());
			break;
		case 7:
			if (Integer.valueOf(String.valueOf(value)) == 0)
				obj.setHsname("不定");
			for (int i = 0; i < businessInfoList.size(); i++)
				if (Integer.valueOf(String.valueOf(value)) == i + 1)
					obj.setHsname(businessInfoList.get(i).getHsname());
			break;
		case 8:
			if (Integer.valueOf(String.valueOf(value)) == 0)
				obj.setGoodsbrandname("不定");
			for (int i = 0; i < brandInfoList.size(); i++)
				if (Integer.valueOf(String.valueOf(value)) == i + 1)
					obj.setGoodsbrandname(brandInfoList.get(i)
							.getGoodsbrandname());
			break;
		case 9:
			obj.setGoodshh(String.valueOf(value));
			break;
		case 10:
			obj.setCkjj(BigDecimal.valueOf(Double.valueOf(value.toString())));
			break;
		case 11:
			obj.setPfj(BigDecimal.valueOf(Double.valueOf(value.toString())));
			break;
		case 12:
			obj.setHyj(BigDecimal.valueOf(Double.valueOf(value.toString())));
			break;
		case 13:
			obj.setLsj(BigDecimal.valueOf(Double.valueOf(value.toString())));
			break;
		case 14:
			obj.setUnit(String.valueOf(value));
			break;
		case 15:
			obj.setGg(String.valueOf(value));
			break;
		}
		getViewer().update(element, null);
	}

	@Override
	public void changeEvent() {
		if (editor instanceof GoodsInfoTextKeyCellEditor)
			((GoodsInfoTextKeyCellEditor) editor).releaseKey();
		if (editor instanceof GoodsInfoComboBoxCellEditor)
			((GoodsInfoComboBoxCellEditor) editor).releaseKey();

	}

	@Override
	public void modify() {
		isFirst = true;
	}

}
